%     AERO LOAD - программа для нагружения агрегатов самолета распределенной нагрузкой
%     Copyright (C) 2018-2024  Михаил Козлов
%     Сайт проекта: <http://www.fastenerdoctor.ru/>
%     Адрес электронной почты для связи с разработчиком:
%     <doctor.fastener@gmail.com>
% 
%     Эта программа является свободным программным обеспечением: Вы можете
%     распространять ее и/или изменять, соблюдая условия Универсальной общественной
%     лицензии GNU, опубликованной Фондом свободного программного обеспечения,
%     либо редакции 3, либо (на Ваше усмотрение) любой редакции, выпущенной позже.
% 
%     Эта программа распространяется в надежде на то, что она окажется полезной,
%     но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, включая даже подразумеваемую гарантию ТОВАРНОЙ ПРИГОДНОСТИ,
%     либо ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Ознакомьтесь с Универсальной общественной
%     лицензией GNU для получения более подробной информации.
% 
%     Вы должны были получить копию Универсальной общественной лицензии GNU
%     вместе с этой программой. Если Вы ее не получили, то перейдите по адресу:
%     <http://www.gnu.org/licenses/>.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <https://www.gnu.org/licenses/>.

function Load = Extend_Distributions(Load)

if (~isempty(Load.Span_Load.Load_Diagram_Data))
    
    if (Load.Span_Load.Load_Diagram_Data(1,1) > 0)
        
        Extrapolated_Value = Access_Array(Load.Span_Load.Load_Diagram_Data, 0, Load.Extrapolation.Span_Load_0, Load.Extrapolation.Span_Load_1);
        
        Load.Span_Load.Load_Diagram_Data(2:size(Load.Span_Load.Load_Diagram_Data,1)+1,:) = Load.Span_Load.Load_Diagram_Data(1:size(Load.Span_Load.Load_Diagram_Data,1),:);
        Load.Span_Load.Load_Diagram_Data(1,1) = 0;
        Load.Span_Load.Load_Diagram_Data(1,2) = Extrapolated_Value;
        
    end
    
    if (Load.Span_Load.Load_Diagram_Data(size(Load.Span_Load.Load_Diagram_Data,1),1) < 1)
        
        Extrapolated_Value = Access_Array(Load.Span_Load.Load_Diagram_Data, 1, Load.Extrapolation.Span_Load_0, Load.Extrapolation.Span_Load_1);
        
        Load.Span_Load.Load_Diagram_Data(size(Load.Span_Load.Load_Diagram_Data,1)+1,1) = 1;
        Load.Span_Load.Load_Diagram_Data(size(Load.Span_Load.Load_Diagram_Data,1),2) = Extrapolated_Value;
        
    end
    
end

if (~isempty(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data))
    
    if (Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(1,1) > 0)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data, 0, Load.Extrapolation.Trapezoid_Xd_0, Load.Extrapolation.Trapezoid_Xd_1);
        
        Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(2:size(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data,1)+1,:) = Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(1:size(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data,1),:);
        Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(1,1) = 0;
        Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(1,2) = Extrapolated_Value;
        
    end
    
    if (Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(size(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data,1),1) < 1)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data, 1, Load.Extrapolation.Trapezoid_Xd_0, Load.Extrapolation.Trapezoid_Xd_1);
        
        Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(size(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data,1)+1,1) = 1;
        Load.Chord_Pressure.Trapezoid_Xd_Distr_Data(size(Load.Chord_Pressure.Trapezoid_Xd_Distr_Data,1),2) = Extrapolated_Value;
        
    end
    
end

if (~isempty(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data))
    
    if (Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(1,1) > 0)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data, 0, Load.Extrapolation.Model_X_p2_p1_0, Load.Extrapolation.Model_X_p2_p1_1);
        
        Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(2:size(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data,1)+1,:) = Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(1:size(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data,1),:);
        Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(1,1) = 0;
        Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(1,2) = Extrapolated_Value;
        
    end
    
    if (Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(size(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data,1),1) < 1)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data, 1, Load.Extrapolation.Model_X_p2_p1_0, Load.Extrapolation.Model_X_p2_p1_1);
        
        Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(size(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data,1)+1,1) = 1;
        Load.Chord_Pressure.Model_X_p2_p1_Distr_Data(size(Load.Chord_Pressure.Model_X_p2_p1_Distr_Data,1),2) = Extrapolated_Value;
        
    end
    
end

if (~isempty(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data))
    
    if (Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(1,1) > 0)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data, 0, Load.Extrapolation.Model_X_p3_p1_0, Load.Extrapolation.Model_X_p3_p1_1);
        
        Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(2:size(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data,1)+1,:) = Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(1:size(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data,1),:);
        Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(1,1) = 0;
        Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(1,2) = Extrapolated_Value;
        
    end
    
    if (Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(size(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data,1),1) < 1)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data, 1, Load.Extrapolation.Model_X_p3_p1_0, Load.Extrapolation.Model_X_p3_p1_1);
        
        Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(size(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data,1)+1,1) = 1;
        Load.Chord_Pressure.Model_X_p3_p1_Distr_Data(size(Load.Chord_Pressure.Model_X_p3_p1_Distr_Data,1),2) = Extrapolated_Value;
        
    end
    
end

if (~isempty(Load.Chord_Pressure.Model_X_p4_p1_Distr_Data))
    
    if (Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(1,1) > 0)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_p4_p1_Distr_Data, 0, Load.Extrapolation.Model_X_p4_p1_0, Load.Extrapolation.Model_X_p4_p1_1);
        
        Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(2:size(Load.Chord_Pressure.Model_X_p4_p1_Distr_Data,1)+1,:) = Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(1:size(Load.Chord_Pressure.Model_X_p4_p1_Distr_Data,1),:);
        Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(1,1) = 0;
        Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(1,2) = Extrapolated_Value;
        
    end
    
    if (Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(size(Load.Chord_Pressure.Model_X_p4_p1_Distr_Data,1),1) < 1)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_p4_p1_Distr_Data, 1, Load.Extrapolation.Model_X_p4_p1_0, Load.Extrapolation.Model_X_p4_p1_1);
        
        Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(size(Load.Chord_Pressure.Model_X_p4_p1_Distr_Data,1)+1,1) = 1;
        Load.Chord_Pressure.Model_X_p4_p1_Distr_Data(size(Load.Chord_Pressure.Model_X_p4_p1_Distr_Data,1),2) = Extrapolated_Value;
        
    end
    
end

if (~isempty(Load.Chord_Pressure.Model_X_f_Distr_Data))
    
    if (Load.Chord_Pressure.Model_X_f_Distr_Data(1,1) > 0)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_f_Distr_Data, 0, Load.Extrapolation.Model_X_f_0, Load.Extrapolation.Model_X_f_1);
        
        Load.Chord_Pressure.Model_X_f_Distr_Data(2:size(Load.Chord_Pressure.Model_X_f_Distr_Data,1)+1,:) = Load.Chord_Pressure.Model_X_f_Distr_Data(1:size(Load.Chord_Pressure.Model_X_f_Distr_Data,1),:);
        Load.Chord_Pressure.Model_X_f_Distr_Data(1,1) = 0;
        Load.Chord_Pressure.Model_X_f_Distr_Data(1,2) = Extrapolated_Value;
        
    end
    
    if (Load.Chord_Pressure.Model_X_f_Distr_Data(size(Load.Chord_Pressure.Model_X_f_Distr_Data,1),1) < 1)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_f_Distr_Data, 1, Load.Extrapolation.Model_X_f_0, Load.Extrapolation.Model_X_f_1);
        
        Load.Chord_Pressure.Model_X_f_Distr_Data(size(Load.Chord_Pressure.Model_X_f_Distr_Data,1)+1,1) = 1;
        Load.Chord_Pressure.Model_X_f_Distr_Data(size(Load.Chord_Pressure.Model_X_f_Distr_Data,1),2) = Extrapolated_Value;
        
    end
    
end

if (~isempty(Load.Chord_Pressure.Model_X_f1_Distr_Data))
    
    if (Load.Chord_Pressure.Model_X_f1_Distr_Data(1,1) > 0)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_f1_Distr_Data, 0, Load.Extrapolation.Model_X_f1_0, Load.Extrapolation.Model_X_f1_1);
        
        Load.Chord_Pressure.Model_X_f1_Distr_Data(2:size(Load.Chord_Pressure.Model_X_f1_Distr_Data,1)+1,:) = Load.Chord_Pressure.Model_X_f1_Distr_Data(1:size(Load.Chord_Pressure.Model_X_f1_Distr_Data,1),:);
        Load.Chord_Pressure.Model_X_f1_Distr_Data(1,1) = 0;
        Load.Chord_Pressure.Model_X_f1_Distr_Data(1,2) = Extrapolated_Value;
        
    end
    
    if (Load.Chord_Pressure.Model_X_f1_Distr_Data(size(Load.Chord_Pressure.Model_X_f1_Distr_Data,1),1) < 1)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_f1_Distr_Data, 1, Load.Extrapolation.Model_X_f1_0, Load.Extrapolation.Model_X_f1_1);
        
        Load.Chord_Pressure.Model_X_f1_Distr_Data(size(Load.Chord_Pressure.Model_X_f1_Distr_Data,1)+1,1) = 1;
        Load.Chord_Pressure.Model_X_f1_Distr_Data(size(Load.Chord_Pressure.Model_X_f1_Distr_Data,1),2) = Extrapolated_Value;
        
    end
    
end

if (~isempty(Load.Chord_Pressure.Model_X_f2_Distr_Data))
    
    if (Load.Chord_Pressure.Model_X_f2_Distr_Data(1,1) > 0)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_f2_Distr_Data, 0, Load.Extrapolation.Model_X_f2_0, Load.Extrapolation.Model_X_f2_1);
        
        Load.Chord_Pressure.Model_X_f2_Distr_Data(2:size(Load.Chord_Pressure.Model_X_f2_Distr_Data,1)+1,:) = Load.Chord_Pressure.Model_X_f2_Distr_Data(1:size(Load.Chord_Pressure.Model_X_f2_Distr_Data,1),:);
        Load.Chord_Pressure.Model_X_f2_Distr_Data(1,1) = 0;
        Load.Chord_Pressure.Model_X_f2_Distr_Data(1,2) = Extrapolated_Value;
        
    end
    
    if (Load.Chord_Pressure.Model_X_f2_Distr_Data(size(Load.Chord_Pressure.Model_X_f2_Distr_Data,1),1) < 1)
        
        Extrapolated_Value = Access_Array(Load.Chord_Pressure.Model_X_f2_Distr_Data, 1, Load.Extrapolation.Model_X_f2_0, Load.Extrapolation.Model_X_f2_1);
        
        Load.Chord_Pressure.Model_X_f2_Distr_Data(size(Load.Chord_Pressure.Model_X_f2_Distr_Data,1)+1,1) = 1;
        Load.Chord_Pressure.Model_X_f2_Distr_Data(size(Load.Chord_Pressure.Model_X_f2_Distr_Data,1),2) = Extrapolated_Value;
        
    end
    
end

if (~isempty(Load.Chord_Pressure.User_Model_Data))
    
    for i=1:size(Load.Chord_Pressure.User_Model_Data.Pressure_Data,1)
        
        if (Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}(1,1) > 0)

            Extrapolated_Value = Access_Array(Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}, 0, Load.Extrapolation.Chord_Pressure_0, Load.Extrapolation.Chord_Pressure_1);

            Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}(2:size(Load.Chord_Pressure.User_Model_Data.Pressure_Data{i},1)+1,:) = Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}(1:size(Load.Chord_Pressure.User_Model_Data.Pressure_Data{i},1),:);
            Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}(1,1) = 0;
            Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}(1,2) = Extrapolated_Value;

        end

        if (Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}(size(Load.Chord_Pressure.User_Model_Data.Pressure_Data{i},1),1) < 1)

            Extrapolated_Value = Access_Array(Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}, 1, Load.Extrapolation.Chord_Pressure_0, Load.Extrapolation.Chord_Pressure_1);

            Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}(size(Load.Chord_Pressure.User_Model_Data.Pressure_Data{i},1)+1,1) = 1;
            Load.Chord_Pressure.User_Model_Data.Pressure_Data{i}(size(Load.Chord_Pressure.User_Model_Data.Pressure_Data{i},1),2) = Extrapolated_Value;

        end
        
    end

    if (Load.Chord_Pressure.User_Model_Data.Span_Coord(1,1) > 0)

        Extrapolated_Distribution = Access_User_Model(0, Load);

        Load.Chord_Pressure.User_Model_Data.Span_Coord(2:size(Load.Chord_Pressure.User_Model_Data.Span_Coord,1)+1,:) = Load.Chord_Pressure.User_Model_Data.Span_Coord(1:size(Load.Chord_Pressure.User_Model_Data.Span_Coord,1),:);
        Load.Chord_Pressure.User_Model_Data.Span_Coord(1,1) = 0;
        
        Load.Chord_Pressure.User_Model_Data.Pressure_Data(2:size(Load.Chord_Pressure.User_Model_Data.Pressure_Data,1)+1,:) = Load.Chord_Pressure.User_Model_Data.Pressure_Data(1:size(Load.Chord_Pressure.User_Model_Data.Pressure_Data,1),:);
        Load.Chord_Pressure.User_Model_Data.Pressure_Data{1,1} = Extrapolated_Distribution;
        
    end

    if (Load.Chord_Pressure.User_Model_Data.Span_Coord(size(Load.Chord_Pressure.User_Model_Data.Span_Coord,1),1) < 1)

        Extrapolated_Distribution = Access_User_Model(1, Load);

        Load.Chord_Pressure.User_Model_Data.Span_Coord(size(Load.Chord_Pressure.User_Model_Data.Span_Coord,1)+1,1) = 1;
        Load.Chord_Pressure.User_Model_Data.Pressure_Data{size(Load.Chord_Pressure.User_Model_Data.Pressure_Data,1)+1,1} = Extrapolated_Distribution;
        
    end
    
end
